// const baseUrl = "https://bbq.qxxpz.top:4443";
const baseUrl = "http://192.144.227.111:8000";

const app = getApp()
function POST(url, params) {
    return new Promise(function (resolve, reject) {
        qq.request({
            url: baseUrl + url,
            data: params,
            header: {
                "content-type": "application/json",
                "Authorization": app.globalData.token
            },
            method: 'POST',
            success: (res) => resolve(res),
            fail: (res) => reject(res),
            complete: () => { qq.hideLoading() }
        })
    });
}

function GET(url, params) {
    return new Promise(function (resolve, reject) {
        qq.request({
            url: baseUrl + url,
            data: params,
            header: {
                "content-type": "application/json",
                "Authorization": app.globalData.token
            },
            method: 'GET',
            success: (res) => resolve(res),
            fail: (res) => reject(res),
            complete: () => { qq.hideLoading() }
        })
    });
}

function DELETE(url, params) {
    qq.showLoading({
        title: '删除中',
        mask: true
    })
    return new Promise(function (resolve, reject) {
        qq.request({
            url: baseUrl + url,
            method: 'DELETE',
            header: {
                "Authorization": app.globalData.token
            },
            data: params,
            success: (res) => resolve(res),
            fail: (res) => reject(res),
            complete: () => { qq.hideLoading() }
        })
    });
}

function UPLOAD(url, fileName, file, params) {
    qq.showLoading({
        title: '上传中',
        mask: true
    })
    return new Promise(function (resolve, reject) {
        qq.uploadFile({
            url: baseUrl + url,
            name: fileName,
            filePath: file,
            formData: params,
            success: (res) => resolve(res),
            fail: (res) => reject(res),
            complete: () => { qq.hideLoading() }
        })
    });
}

function DOWNLOAD(url) {
    return new Promise(function (resolve, reject) {
        console.log(baseUrl + url)
        qq.downloadFile({
            url: baseUrl + url,
            success: (res) => resolve(res),
            fail: (res) => reject(res),
            complete: () => { qq.hideLoading() }
        })
    });
}

module.exports = {
    POST: POST,
    GET: GET,
    DELETE: DELETE,
    UPLOAD: UPLOAD,
    DOWNLOAD: DOWNLOAD,
}